{include file="public/head"/}
<style>
  .main-content {
    margin-left: 0px;
    min-height: 80%;
  }

  .row .cf {
    margin-bottom: 10px;
  }
</style>


<div class="main-container" id="main-container">
  <script type="text/javascript">
    try {
      ace.settings.check('main-container', 'fixed')
    } catch (e) {
    }
  </script>

  <div class="main-container-inner">
    <a class="menu-toggler" id="menu-toggler" href="#">
      <span class="menu-text"></span>
    </a>
    {include file="public/nav"/}

    <div class="main-content">
      <div class="page-content">
        <div class="row">
          <div class="well">
            <h1 class=" lighter smaller">
              <span class="blue bigger-125">
                <i class="ace-icon fa fa-th-list"></i>
              </span>
              待结账工单
              <a style="font-size:13px;" href="{:url('main/modulefront')}">返回首页</a>
            </h1>
            <hr>
            <div class="space"></div>

            <div class="row-fluid">
              <!--<h3 class="lighter block green">选择或添加客户信息</h3>-->
              <!--<a class="btn btn-info" href="#modal-fmWork" data-toggle="modal">
                <i class="icon-ok bigger-110"></i>
                添加客户
              </a>-->
              <!--数据筛选表单-->
              <form class="form-inline fmWorkQuery-hook">
                <input class="input-small" name="num" placeholder="工单号" />
                <input class="input-large" name="car_num" placeholder="车牌号" />
                <input class="input-small" name="client_name" placeholder="客户姓名" />
                <button type="button" class="btn btn-info btn-sm" onclick="indexInvoice.search();">
                  <i class="fa fa-search bigger-110"></i>
                  查找
                </button>
                <button type="button" class="btn btn-primary btn-sm" onclick="indexInvoice.searchNull();">
                  <i class="fa fa-remove bigger-110"></i>
                  清空
                </button>
              </form>
              <!--客户数据表-->
              <table id="tbWork"></table>
              <!--客户表页码内容-->
              <div id="tbWorkPager"></div>

              <!--结账模态框-->
              <div id="modal-fmWorkPay" class="modal" tabindex="-1">
                <div class="modal-dialog">
                  <div class="modal-content">
                    <div class="modal-header">
                      <button type="button" class="close" data-dismiss="modal">&times;</button>
                      <h4 class="blue bigger">结账&nbsp;(
                        <span style="color:red;" id="showNum"></span>)</h4>
                    </div>
                    <div class="modal-body overflow-visible">
                      <!--添加客户表单-->
                      <form class="form-horizontal" id="fmWorkPay" method="post">
                        <input type="hidden" name="id" value="">
                        <input type="hidden" name="car_num" value="">
                        <div class="form-group">
                          <label class="col-sm-3 control-label no-padding-right">会员余额</label>
                          <div class="col-sm-9">
                            <input data="moneyleft" class="col-xs-10 col-sm-5" value="" readonly>
                            <span class="help-inline col-xs-12 col-sm-7">
                              <span class="middle"></span>
                            </span>
                          </div>
                        </div>
                        <div class="form-group">
                          <label class="col-sm-3 control-label no-padding-right">已消费总额</label>
                          <div class="col-sm-9">
                            <input data="moneyconsumption" class="col-xs-10 col-sm-5" value="" readonly>
                            <span class="help-inline col-xs-12 col-sm-7">
                              <span class="middle"></span>
                            </span>
                          </div>
                        </div>
                        <div class="form-group">
                          <label class="col-sm-3 control-label no-padding-right">会员类型</label>
                          <div class="col-sm-9">
                            <input data="vip" class="col-xs-10 col-sm-5" value="" readonly>
                            <span class="help-inline col-xs-12 col-sm-7">
                              <span class="middle"></span>
                            </span>
                          </div>
                        </div>
                        <div class="form-group">
                          <label class="col-sm-3 control-label no-padding-right">小计</label>
                          <div class="col-sm-9">
                            <input name="sale" class="col-xs-10 col-sm-5" value="" readonly>
                            <span class="help-inline col-xs-12 col-sm-7">
                              <span class="middle"></span>
                            </span>
                          </div>
                        </div>
                        <!-- <div class="form-group">
                          <label class="col-sm-3 control-label no-padding-right">折后</label>
                          <div class="col-sm-9">
                            <input name="sale_discount" class="col-xs-10 col-sm-5" value="" readonly>
                            <span class="help-inline col-xs-12 col-sm-7">
                              <input type="checkbox" id="dis"> 不打折
                              <span class="middle"></span>
                            </span>
                          </div>
                        </div> -->
                        <input type="hidden" name="sale_discount" class="col-xs-10 col-sm-5" value="" readonly>
                        <div class="form-group">
                          <label class="col-sm-3 control-label no-padding-right">税费</label>
                          <div class="col-sm-9">
                            <input name="tax" type="number" class="col-xs-10 col-sm-5" value="">
                            <span class="help-inline col-xs-12 col-sm-7">
                              <span class="middle"></span>
                            </span>
                          </div>
                        </div>
                        <div class="form-group">
                          <label class="col-sm-3 control-label no-padding-right">总计</label>
                          <div class="col-sm-9">
                            <input name="total" class="col-xs-10 col-sm-5" value="" readonly>
                            <span class="help-inline col-xs-12 col-sm-7">
                              <span class="middle"></span>
                            </span>
                          </div>
                        </div>
                        <div class="form-group">
                          <label class="col-sm-3 control-label no-padding-right"> 结账方式 </label>
                          <div class="col-sm-9">
                            <div class="radio">
                              <label>
                                <input name="pay_type" type="checkbox" class="ace" value="微信">
                                <span class="lbl"> 微信 </span>
                              </label>&nbsp;&nbsp;
                              <label>
                                <input name="pay_type" type="checkbox" class="ace" value="支付宝">
                                <span class="lbl"> 支付宝 </span>
                              </label>&nbsp;&nbsp;
                              <label>
                                <input name="pay_type" type="checkbox" class="ace" value="现金">
                                <span class="lbl"> 现金 </span>
                              </label>&nbsp;&nbsp;
                              <label>
                                <input name="pay_type" type="checkbox" class="ace" value="刷卡">
                                <span class="lbl"> 刷卡 </span>
                              </label>&nbsp;&nbsp;
                              <label>
                                <input name="pay_type" type="checkbox" class="ace" value="余额">
                                <span class="lbl"> 余额 </span>
                              </label>
                            </div>
                            <span class="help-inline col-xs-12 col-sm-7">
                              <span class="middle"></span>
                            </span>
                          </div>
                        </div>
                      </form>
                    </div>

                    <div class="modal-footer">
                      <button class="btn btn-sm" data-dismiss="modal">
                        <i class="icon-remove"></i>
                        取消
                      </button>
                      <button class="btn btn-sm btn-primary" onclick="indexInvoice.doPay(this);" data-loading-text="保存中...">
                        <i class="icon-ok"></i>
                        保存
                      </button>
                    </div>
                  </div>
                </div>
              </div>
            </div>
            <hr>
            <div class="space"></div>
          </div>
        </div>
        <!-- /.row -->
      </div>
      <!-- /.page-content -->
    </div>
    <!-- /.main-content -->

  </div>
  <!-- /.main-container-inner -->

  <a href="#" id="btn-scroll-up" class="btn-scroll-up btn btn-sm btn-inverse">
    <i class="icon-double-angle-up icon-only bigger-110"></i>
  </a>
</div>
<!-- /.main-container -->
{include file="public/footerjs"/}

<script>
  $(function () {
    $('#fmWorkPay input[name=tax]').keyup(function () {
      var tax = $('#fmWorkPay input[name=tax]').val();
      var sale_discount = $('#fmWorkPay input[name=sale_discount]').val();
      $('#fmWorkPay input[name=total]').val((Number(sale_discount) + Number(tax)).toFixed(2));
    });
    $("#dis").click(function () {
      var sale = $('#fmWorkPay input[name=sale]').val();
      var vip = $('#fmWorkPay input[data=vip]').val();
      var tax = $('#fmWorkPay input[name=tax]').val();
      var sale_discount = Number(vip == 'A' ? sale * 0.9 : vip == 'B' ? sale * 0.95 : sale).toFixed(2);
      if ($("#dis").is(':checked')) {
        sale_discount = sale;
      }
      $('#fmWorkPay input[name=sale_discount]').val(sale_discount);

      $('#fmWorkPay input[name=total]').val((Number(sale_discount) + Number(tax)).toFixed(2));
    });
    //加载数据
    var $tbWork = $('#tbWork'), $pager = $('#tbWorkPager');
    $tbWork.jqGrid({
      url: "{:url('work/getWorkPaied')}",
      datatype: 'json',
      mttype: 'post',
      colNames: ['工单号', '车牌号', '客户', '接待人', '操作'],
      colModel: [
        { name: 'num', index: 'num', width: '100', align: 'center' },
        { name: 'car_num', index: 'car_num', width: '100', align: 'center' },
        { name: 'client_name', index: 'client_name', width: '100', align: 'center' },
        { name: 'reception_name', index: 'reception_name', width: '100', align: 'center' },
        {
          name: 'id', index: 'id', width: '100', align: 'center',
          formatter: function (value, options, obj) {
            var res = '<a href="/admin/invoice/index/id/' + value + '" target="_blank" style="cursor:pointer;">明细</a>';
            if (obj.pay_result == 1) {
              res += '&nbsp;&nbsp;<a id="btnPay' + value + '" onclick="indexInvoice.pay(' + value + ',\'' + obj.sale + '\',\'' + obj.car_num + '\')" style="cursor:pointer;">结账</a>';
            }
            return res;
          }
        },
      ],
      height: 'auto',
      pager: '#tbWorkPager',
      viewrecords: true,
      rowNum: 10,
      rowList: [10, 20, 40],
      autowidth: true
    });

    // 隐藏水平方向滚动条
    $tbWork.closest(".ui-jqgrid-bdiv").css({ "overflow-x": "hidden" });
  });
  indexInvoice = {
    search: function () {
      var $fmQuery = $('.fmWorkQuery-hook');
      var param = ittaoli.serializeObject($fmQuery);
      $('#tbWork').jqGrid('setGridParam', {
        postData: { q: JSON.stringify(param) },
        page: 1
      }).trigger("reloadGrid");
    },
    searchNull: function () {
      $('.fmWorkQuery-hook input').val('');
      var param = {};
      $('#tbWork').jqGrid('setGridParam', {
        postData: { q: JSON.stringify(param) },
        page: 1
      }).trigger("reloadGrid");
    },
    pay: function (id, sale, car_num) {
      $("#showNum").text(car_num);
      $('#modal-fmWorkPay').modal('show');
      $('#fmWorkPay input[name=id]').val(id);
      $('#fmWorkPay input[name=car_num]').val(car_num);
      $('#fmWorkPay input[name=sale]').val(sale);
      $('#fmWorkPay input[name=tax]').val('');
      $("#fmWorkPay input[name=pay_type]:checkbox").attr("checked", false);

      $.post("{:url('car/findByNum')}", { car_num: car_num }, function (data) {
        $('#fmWorkPay input[data=moneyleft]').val(data.moneyleft);
        $('#fmWorkPay input[data=moneyconsumption]').val(data.moneyconsumption);
        $('#fmWorkPay input[data=vip]').val(data.vip);
        sale_discount = sale;
        // var sale_discount = data.vip == 'A' ? sale * 0.9 : data.vip == 'B' ? sale * 0.95 : sale;
        $('#fmWorkPay input[name=sale_discount]').val(Number(sale_discount).toFixed(2));
        $('#fmWorkPay input[name=total]').val(Number(sale_discount).toFixed(2));
      });
    },
    doPay: function (obj) {
      var param = ittaoli.serializeObject($('#fmWorkPay'));
      if (!param.pay_type) {
        bootbox.alert('请选择支付方式！');
        return;
      }
      var moneyleft = Number($('#fmWorkPay input[data=moneyleft]').val());
      if (moneyleft > 0 && moneyleft < Number(param.total)) {
        alert('会员余额不足，应再收：￥' + (Number(param.total) - Number(moneyleft)).toFixed(2));
      }

      $(obj).button('loading');
      $.post('{:url("work/doPay")}', { param: JSON.stringify(param) }, function (data) {
        if (data > 0) {
          bootbox.alert("结账成功！");

          setTimeout(function () {
            var url = "{:url('invoice/index')}"
            url = '<a href="' + url + '/id/' + param.id + '" rel="external" target="_blank">订单详情</a>';
            var a = $(url).get(0);
            var e = document.createEvent('MouseEvents');
            e.initEvent('click', true, true);
            a.dispatchEvent(e);
            location.href = "{:url('main/modulefront')}";
          }, 1000);

        } else {
          bootbox.alert('结账失败！');
        }
        $(obj).button('reset');
      })
    }
  };
</script> {include file="public/foot"/}